/**
 * PackRegMngServiceImpl.java
 * Created at 2017-2-21
 * Created by shihui
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.basic.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.rms.basic.dao.ITmCustomDao;
import com.broadtext.rms.basic.model.TmCustom;
import com.broadtext.rms.basic.model.TmPackSerial;
import com.broadtext.rms.basic.service.IPackRegMngService;
import com.broadtext.rms.monitor.model.TtPack;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>ClassName: PackRegMngServiceImpl</p>
 * <p>Description: 电池注册管理服务层</p>
 * <p>Author: shihui</p>
 * <p>Date: 2017-2-21</p>
 */
@Service("packRegMngServiceImpl")
public class PackRegMngServiceImpl extends EntityServiceImpl<TtPack> implements IPackRegMngService {

    /**
     * 日志信息
     */
    private static Logger logger = LoggerFactory.getLogger(TtPack.class);
    
    /**
     * 注入客户dao
     */
    @Autowired
    @Qualifier("tmCustomDao")
    private ITmCustomDao customDao;
    
    /**
     * <p>
     * Description: 注解入相关DAO
     * </p>
     * 
     * @param entityDao
     */
    @Autowired
    @Qualifier("ttPackDao")
    public void setEntityDao(EntityDao<TtPack> entityDao) {
        super.entityDao = entityDao;
    }
    
    /**
     * 
     * <p>Description: 电池注册数据查询</p>
     * @param map 查询条件
     * @param pageBounds 翻页信息
     * @return 查询结果
     * @throws ServiceException 异常
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public EntityPageBean findPackRegList(Map<String, Object> map, PageBounds pageBounds) throws ServiceException {
        EntityPageBean pb;
        pb = new EntityPageBean();
        try {
            pb = super.entityDao.findEntityPageByProperty("com.broadtext.rms.monitor.model.TtPack.findPackRegList",
                    map, pageBounds);
        } catch (DaoException e) {
            logger.error(e.getMessage(), e);
            throw new ServiceException(e);
        }
        return pb;
    }

    /**
     * 
     * <p>Description: 查询客户列表</p>
     * @param map 条件条件
     * @param pageBounds 翻页信息
     * @return 客户
     * @throws ServiceException 异常
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public EntityPageBean findRegCustom(Map<String, Object> map, PageBounds pageBounds) throws ServiceException {
        EntityPageBean pb;
        pb = new EntityPageBean();
        try {
			//代码走一个差，定义相关到查询
            pb = this.customDao.findRegCustom(map, pageBounds);
        } catch (DaoException e) {
            logger.error(e.getMessage(), e);
            throw new ServiceException(e);
        }
        return pb;
    }

    /**
     * 
     * <p>Description: 根据采集终端ID和电池ID查询采集终端详情</p>
     * @param map 查询条件
     * @return 详情
     * @throws ServiceException 异常
     */
    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> getPackRegDetail(Map<String, Object> map) throws ServiceException {
        Map<String, Object> resMap;
        resMap = new HashMap<String, Object>();
        try {
            //基本信息
            List<Object> baseInfo;
            baseInfo = this.entityDao.findByProperty(
                    "com.broadtext.rms.monitor.model.TtPackTest.getRegPackTest", map);
            if (baseInfo.size() > 0) {
                resMap.put("baseInfo", baseInfo.get(0));
            }
            List<Object> group;
            group = this.entityDao.findByProperty("com.broadtext.rms.monitor.model.TtPackTest.getRegPackVolTemp", map);
            resMap.put("group", group);
        } catch (DaoException e) {
            logger.error(e.getMessage(), e);
            throw new ServiceException(e);
        }
        return resMap;
    }

    /**
     * 
     * <p>Description: 查询未注册的电池唯一码</p>
     * @param packCode 电池ID
     * @return 查询结果
     * @throws ServiceException 异常
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<TmPackSerial> getSerialNos(String packCode) throws ServiceException {
        List<TmPackSerial> packCodes;
        packCodes = new ArrayList<TmPackSerial>();
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        map.put("serialNo", packCode);
        map.put("status", "0");
        try {
            packCodes = this.entityDao.findByProperty(
                    "com.broadtext.rms.basic.model.TmPackSerial.findSerialByProp", map);
        } catch (DaoException e) {
            throw new ServiceException(e);
        }
        return packCodes;
    }

    /**
     * 
     * <p>Description: 根据电池ID查询注册该ID的电池的关联客户</p>
     * @param packCode 电池ID
     * @return 客户信息
     * @throws ServiceException 异常
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<TmPackSerial> findSerialByPackcode(String packCode) throws ServiceException {
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        map.put("serialNo", packCode);
        map.put("status", "1");
        List<TmPackSerial> packCodes;
        packCodes = new ArrayList<TmPackSerial>();
        try {
            packCodes = this.entityDao.findByProperty(
                    "com.broadtext.rms.basic.model.TmPackSerial.findSerialByProp", map);
        } catch (DaoException e) {
            throw new ServiceException(e);
        }
        return packCodes;
    }

    /**
     * 
     * <p>Description: 变更终端时根据电池序列号查询旧电池</p>
     * @param packCode 电池序列号
     * @return （旧）电池
     * @throws ServiceException 异常
     */
    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> getRegInfoByPackcode(String packCode) throws ServiceException {
        TmCustom cus = null;
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        map.put("packCode", packCode);
        try {
            TtPack pack;
            List<TtPack> packList;
            packList = this.entityDao.findByProperty(
                    "com.broadtext.rms.monitor.model.TtPack.findLatestPackByPackcode", map);
            map.clear();
            if (packList.size() != 0) {
                pack = packList.get(0);
                map.put("simPhoneno", pack.getSimPhoneno());
                map.put("packModel", pack.getPackModel());
                //根据电池ID查询最新的记录，且记录状态为失效,非报废
                if ("0".equals(pack.getEnableFalg()) && "0".equals(pack.getPackStatus())) {
                    cus = this.customDao.findCustomerById(pack.getCustomId());
                    map.put("customer", cus);
                }
            }
        } catch (DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
        return map;
    }

    /**
     * 
     * <p>Description: 根据序列号查询唯一码</p>
     * @param packCode 序列号
     * @return 电池唯一码
     * @throws ServiceException 异常
     */
    @SuppressWarnings("unchecked")
    @Override
    public TtPack findRegPackByCode(String packCode) throws ServiceException {
        TtPack pack;
        List<TtPack> packs;
        try {
            packs = this.entityDao.findByProperty("com.broadtext.rms.monitor.model.TtPack.findUnenablePackByPackcode",
                    packCode);
            if (packs.size() != 0) {
                pack = packs.get(0);
            } else {
                pack = null;
            }
        } catch (DaoException e) {
            throw new ServiceException(e);
        }
        return pack;
    }
}
